package com.ebring.shareEducation.utils;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class SendSMS {
	public static int sendSMS(String iphone,String yzm) throws HttpException, IOException{
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://160.19.212.218:8080/eums/send_strong.do");
		String now=getStringDate();
		
		String key= Md5Utils.GetMD5Code(Md5Utils.GetMD5Code("68hp3fy")+now);
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
		post.setRequestHeader("Connection", "close"); 
		NameValuePair[] data = { new NameValuePair("name", "sxyjyzm"),
				new NameValuePair("seed", now),
				new NameValuePair("key", key),
				new NameValuePair("dest", iphone),
				new NameValuePair("content", "【银信科技】您在使用便捷缴费手机应用，验证码"+yzm+"，2分钟内有效。") };
		System.out.println(yzm);
		post.setRequestBody(data);
		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println(h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
        System.out.println("========>"+result);
		post.releaseConnection();
		return 1;
	}

	public static String getSjs() {
			String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了

			Random rand = new Random();

			StringBuffer flag = new StringBuffer();

			for (int j = 0; j < 6; j++)

			{

				flag.append(sources.charAt(rand.nextInt(9)) + "");
			}

			return flag.toString();
	}
	public static String getStringDate() {
		   Date currentTime = new Date();
		   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		   String dateString = formatter.format(currentTime);
		   return dateString;
		}
}
